#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <std_msgs/String.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
#include <tcr100/ImuData.h>
#include "roscommunicate.h"
#include "stdsoap2.h"
#include "math.h"
#include <vector>
double x = 0.0;
double y = 0.0;
double th = 0.0;
double vx = 0.0;
double vy = 0.0;
double vth = 0.0;

double vx_odom = 0.0;
double vy_odom = 0.0;
double vth_odom = 0.0;
double wheel_vel_left = 0.0;

void callback(const geometry_msgs::Twist& cmd_vel)
{
    vx = cmd_vel.linear.x;
    vy= cmd_vel.linear.y;
    vth= cmd_vel.angular.z;
}
void imucallback(const tcr100::ImuData& msg)
{
    th=msg.yaw;
    vth_odom= msg.th_vel;
}
int main(int argc, char** argv)
{
    ros::init(argc, argv, "basecontroller");
    ros::NodeHandle n;
    ros::Time current_time,last_time;
    ros::Publisher odom_pub = n.advertise<nav_msgs::Odometry>("odom", 50);
    ros::Subscriber sub= n.subscribe("cmd_vel",1000,callback);
    ros::Subscriber imu_sub= n.subscribe("imu_data",1000,imucallback);

    current_time = ros::Time::now();
    last_time = ros::Time::now();

    ros::Rate r(10.0);
    tf::TransformBroadcaster odom_broadcaster;

    string str="127.0.0.1:5050";
    RosCommunicate *rosCommnicate=new RosCommunicate(str);


    while(n.ok())
    {

        UserInfo userInfo;
        int robotIdIn=1;
        int returnFlag;
        if(1==rosCommnicate->vehicleAuto(userInfo,robotIdIn,vx,vy,vth,returnFlag))
        {
           //ROS_INFO("vehicle set vel command sucess \n");
        }
        else
        {
            //ROS_INFO("vehicle set vel command error \n");
        }

        OdemeterData odemeterDataReturn;
        if(1==rosCommnicate->getOdometer(userInfo,robotIdIn,odemeterDataReturn))
        {
            vx_odom=odemeterDataReturn.vel.x_move;//m/s
            vy_odom=odemeterDataReturn.vel.y_move;//m/s

            //ROS_INFO("getOdometer sucess \n");
        }
        else
        {

            //ROS_INFO("getOdometer error \n");
        }

        std::vector<double> wheelMoveSpeed;
        if(1==rosCommnicate->getWheelMoveSpeed(userInfo,robotIdIn,wheelMoveSpeed))
        {
            wheel_vel_left=wheelMoveSpeed[0];//deg/s

            //ROS_INFO("getOdometer sucess \n");
        }
        else
        {

            //ROS_INFO("getOdometer error \n");
        }

        ros::spinOnce();               // check for incoming messages
        current_time = ros::Time::now();
        double dt=(current_time -last_time).toSec();
        double delta_x=(vx_odom*cos(th)-vy_odom*sin(th))*dt;
        double delta_y=(vx_odom*sin(th)+vy_odom*cos(th))*dt;
        x+=delta_x;
        y+=delta_y;

        //compute odometry in a typical way given the velocities of the robot
        geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);
        //first, we'll publish the transform over tf
        geometry_msgs::TransformStamped odom_trans;
        odom_trans.header.stamp = current_time;
        odom_trans.header.frame_id = "odom";
        odom_trans.child_frame_id = "base_footprint";

        odom_trans.transform.translation.x = x;
        odom_trans.transform.translation.y = y;
        odom_trans.transform.translation.z = 0.0;
        odom_trans.transform.rotation = odom_quat;
        //send the transform
        odom_broadcaster.sendTransform(odom_trans);
        //next, we'll publish the odometry message over ROS
        nav_msgs::Odometry odom;
        odom.header.stamp = current_time;
        odom.header.frame_id = "odom";
        //set the position
        odom.pose.pose.position.x = x;
        odom.pose.pose.position.y = y;
        odom.pose.pose.position.z = 0.0;
        odom.pose.pose.orientation = odom_quat;
        //set the velocity
        odom.child_frame_id = "base_footprint";
        odom.twist.twist.linear.x = vx_odom;
        odom.twist.twist.linear.y = vy_odom;
        odom.twist.twist.angular.z = vth_odom;
        //publish the message
        odom_pub.publish(odom);
        last_time=current_time;
        r.sleep();
    }
}
